package org.mebusw.davincicode.unittest;

import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;
import junit.framework.TestCase;
import org.mebusw.davincicode.PlayerManager;
import org.mebusw.davincicode.vo.DavinciCard;
import org.mebusw.davincicode.vo.DavinciPlayer;
import org.mebusw.vo.Player;

@SuppressWarnings("unused")
public class TestPlayerManager extends TestCase {

	private PlayerManager op;
	private DavinciCard newCard;

	public TestPlayerManager(String name) {
		super(name);
	}

	protected void setUp() throws Exception {
		super.setUp();
		op = new PlayerManager(new DavinciPlayer("tester",
				new ArrayList<DavinciCard>()));
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testInsertSort_black_numbercard_1() {
		op.getPlayer().getHand().clear();
		op.insert(new DavinciCard(DavinciCard.BLACK, 3, DavinciCard.NONJOKER,
				DavinciCard.UNKNOWN_CARDID, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 0);
		op.insert(new DavinciCard(DavinciCard.WHITE, 5, DavinciCard.NONJOKER,
				DavinciCard.UNKNOWN_CARDID, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 1);

		newCard = new DavinciCard(DavinciCard.BLACK, 5, DavinciCard.NONJOKER,
				DavinciCard.UNKNOWN_CARDID, DavinciCard.MASKED,
				DavinciCard.NO_OWNER);
		Assert.assertTrue(op.validatePos(newCard, 1));
		op.insert(newCard, 1);
		Assert.assertEquals(3, op.getPlayer().getHand().size());
		// Assert.assertEquals("target", op.get(1).getCardId());
	}

	public void testInsertSort_black_numbercard_2() {
		op.insert(new DavinciCard(DavinciCard.BLACK, 3, DavinciCard.NONJOKER,
				DavinciCard.UNKNOWN_CARDID, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 0);
		op.insert(new DavinciCard(DavinciCard.WHITE, 6, DavinciCard.NONJOKER,
				DavinciCard.UNKNOWN_CARDID, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 1);

		newCard = new DavinciCard(DavinciCard.BLACK, 5, DavinciCard.NONJOKER,
				DavinciCard.UNKNOWN_CARDID, DavinciCard.MASKED,
				DavinciCard.NO_OWNER);
		Assert.assertTrue(op.validatePos(newCard, 1));
		op.insert(newCard, 1);
		Assert.assertEquals(3, op.getPlayer().getHand().size());
		// Assert.assertEquals("target", op.get(1).getCardId());
	}

	public void testValidatePos_wo_joker_1() {
		op.insert(new DavinciCard(DavinciCard.BLACK, 3, DavinciCard.NONJOKER,
				3, DavinciCard.MASKED, DavinciCard.NO_OWNER), 0);
		op.insert(new DavinciCard(DavinciCard.BLACK, 4, DavinciCard.NONJOKER,
				4, DavinciCard.MASKED, DavinciCard.NO_OWNER), 1);

		boolean result = op.validatePos(new DavinciCard(DavinciCard.WHITE, 3,
				DavinciCard.NONJOKER, 13, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 1);
		Assert.assertEquals(true, result);
	}

	public void testValidatePos_wo_joker_2() {
		op.insert(new DavinciCard(false, 3, false, 13, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 0);
		op.insert(new DavinciCard(true, 4, false, 4, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 1);

		boolean result = op.validatePos(new DavinciCard(false, 3, false, 3,
				DavinciCard.MASKED, DavinciCard.NO_OWNER), 1);
		Assert.assertEquals(false, result);
	}

	public void testValidatePos_w_joker_1() {

		op.insert(new DavinciCard(true, 3, false, 3, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 0);
		op.insert(new DavinciCard(true, -1, true, 9, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 1);
		op.insert(new DavinciCard(false, -1, true, 19, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 2);
		op.insert(new DavinciCard(true, 4, false, 4, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 3);

		boolean result = op.validatePos(new DavinciCard(false, 3, false, 13,
				DavinciCard.MASKED, DavinciCard.NO_OWNER), 2);
		Assert.assertEquals(true, result);
	}

	public void testValidatePos_zero() {

		boolean result = op.validatePos(new DavinciCard(DavinciCard.WHITE, 3,
				false, 13, DavinciCard.MASKED, DavinciCard.NO_OWNER), 0);
		Assert.assertTrue(result);
	}

	public void testValidatePos_one_after() {

		op.insert(new DavinciCard(DavinciCard.BLACK, 3, false, 3,
				DavinciCard.MASKED, DavinciCard.NO_OWNER), 0);

		newCard = new DavinciCard(DavinciCard.WHITE, 3, false, 13,
				DavinciCard.MASKED, DavinciCard.NO_OWNER);
		Assert.assertFalse(op.validatePos(newCard, 0));
		Assert.assertTrue(op.validatePos(newCard, 1));
	}

	public void testValidatePos_one_before() {

		op.insert(new DavinciCard(DavinciCard.WHITE, 3, false, 3,
				DavinciCard.MASKED, DavinciCard.NO_OWNER), 0);

		newCard = new DavinciCard(DavinciCard.BLACK, 3, false, 13,
				DavinciCard.MASKED, DavinciCard.NO_OWNER);
		Assert.assertTrue(op.validatePos(newCard, 0));
		Assert.assertFalse(op.validatePos(newCard, 1));
		Assert.assertFalse(op.validatePos(newCard, 2));
		Assert.assertFalse(op.validatePos(newCard, -1));
	}

	public void testValidatePos_outOfBound() {

		newCard = new DavinciCard(DavinciCard.BLACK, 3, false, 13,
				DavinciCard.MASKED, DavinciCard.NO_OWNER);
		Assert.assertFalse(op.validatePos(newCard, 2));
		Assert.assertFalse(op.validatePos(newCard, -1));
	}

	public void testValidatePos_w_joker_2() {

		op.insert(new DavinciCard(true, 3, false, 3, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 0);
		op.insert(new DavinciCard(true, -1, true, 9, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 1);
		op.insert(new DavinciCard(false, -1, true, 19, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 2);
		op.insert(new DavinciCard(true, 4, false, 4, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 3);

		boolean result = op.validatePos(new DavinciCard(false, 3, false, 13,
				DavinciCard.MASKED, DavinciCard.NO_OWNER), 3);
		Assert.assertEquals(true, result);
	}

	public void testValidatePos_w_joker_3() {

		op.insert(new DavinciCard(true, -1, true, 9, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 0);
		op.insert(new DavinciCard(false, -1, true, 19, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 1);

		boolean result = op.validatePos(new DavinciCard(false, 3, false, 13,
				DavinciCard.MASKED, DavinciCard.NO_OWNER), 1);
		Assert.assertEquals(true, result);
	}

	public void testValidatePos_w_joker_4() {

		op.insert(new DavinciCard(true, -1, true, 9, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 0);
		op.insert(new DavinciCard(false, -1, true, 19, DavinciCard.MASKED,
				DavinciCard.NO_OWNER), 1);

		boolean result = op.validatePos(new DavinciCard(false, 3, false, 13,
				DavinciCard.MASKED, DavinciCard.NO_OWNER), 0);
		Assert.assertEquals(true, result);
	}

	public void testInsertSort_black_numbercard_1a() {

		op.insertSort(new DavinciCard(DavinciCard.BLACK, 3,
				DavinciCard.NONJOKER, -1, DavinciCard.MASKED,
				DavinciCard.NO_OWNER));
		op.insertSort(new DavinciCard(DavinciCard.WHITE, 5,
				DavinciCard.NONJOKER, -1, DavinciCard.MASKED,
				DavinciCard.NO_OWNER));

		newCard = new DavinciCard(DavinciCard.BLACK, 5, DavinciCard.NONJOKER,
				0, DavinciCard.MASKED, DavinciCard.NO_OWNER);
		op.insertSort(newCard);
		Assert.assertEquals(3, op.getPlayer().getHand().size());
		Assert.assertEquals(3, ((DavinciCard) op.getPlayer().getHand().get(0))
				.getValue());
		Assert.assertEquals(5, ((DavinciCard) op.getPlayer().getHand().get(1))
				.getValue());
		Assert.assertEquals(DavinciCard.BLACK, ((DavinciCard) op.getPlayer()
				.getHand().get(1)).isBlack());
		Assert.assertEquals(5, ((DavinciCard) op.getPlayer().getHand().get(2))
				.getValue());
		Assert.assertEquals(DavinciCard.WHITE, ((DavinciCard) op.getPlayer()
				.getHand().get(2)).isBlack());
	}

	public void testInsertSort_black_numbercard_2a() {

		op.insertSort(new DavinciCard(DavinciCard.BLACK, 3,
				DavinciCard.NONJOKER, -1, DavinciCard.MASKED,
				DavinciCard.NO_OWNER));
		op.insertSort(new DavinciCard(DavinciCard.WHITE, 5,
				DavinciCard.NONJOKER, -1, DavinciCard.MASKED,
				DavinciCard.NO_OWNER));

		newCard = new DavinciCard(DavinciCard.BLACK, 11, DavinciCard.NONJOKER,
				0, DavinciCard.MASKED, DavinciCard.NO_OWNER);
		op.insertSort(newCard);
		Assert.assertEquals(3, op.getPlayer().getHand().size());
		Assert.assertEquals(3, ((DavinciCard) op.getPlayer().getHand().get(0))
				.getValue());
		Assert.assertEquals(5, ((DavinciCard) op.getPlayer().getHand().get(1))
				.getValue());
		Assert.assertEquals(11, ((DavinciCard) op.getPlayer().getHand().get(2))
				.getValue());
	}

	public void testReinsertSortLast() {

		newCard = new DavinciCard(DavinciCard.BLACK, 11, DavinciCard.NONJOKER,
				0, DavinciCard.MASKED, DavinciCard.NO_OWNER);
		op.insertLast(newCard);
		Assert.assertEquals(11, op.getLast().getValue());
		newCard = new DavinciCard(DavinciCard.BLACK, 0, DavinciCard.NONJOKER,
				0, DavinciCard.MASKED, DavinciCard.NO_OWNER);
		op.insertLast(newCard);
		Assert.assertEquals(0, op.getLast().getValue());
		op.reinsertSortLast();
		Assert.assertEquals(0, ((DavinciCard) op.getPlayer().getHand().get(0))
				.getValue());
		Assert.assertEquals(11, op.getLast().getValue());

	}

	public void testGetStandingCardCount() {

		op.insertSort(new DavinciCard(DavinciCard.BLACK, 3,
				DavinciCard.NONJOKER, -1, DavinciCard.MASKED,
				DavinciCard.NO_OWNER));
		op.insertSort(new DavinciCard(DavinciCard.WHITE, 5,
				DavinciCard.NONJOKER, -1, DavinciCard.MASKED,
				DavinciCard.NO_OWNER));
		op.insertSort(new DavinciCard(DavinciCard.WHITE, 2,
				DavinciCard.NONJOKER, -1, DavinciCard.MASKED,
				DavinciCard.NO_OWNER));

		((DavinciCard) op.getPlayer().getHand().get(0))
				.setRevealed(DavinciCard.MASKED);
		((DavinciCard) op.getPlayer().getHand().get(1))
				.setRevealed(DavinciCard.REVEALED);
		((DavinciCard) op.getPlayer().getHand().get(2))
				.setRevealed(DavinciCard.MASKED);
		Assert.assertEquals(DavinciCard.REVEALED, ((DavinciCard) op.getPlayer()
				.getHand().get(1)).isRevealed());
		Assert.assertEquals(3, op.getPlayer().getHand().size());
		Assert.assertEquals(2, op.getStandingCardCount());
	}

}
